import 'package:flutter/material.dart';
import 'package:heven/api/index.dart';
import 'package:heven/model/apple_login_model.dart';
import 'package:heven/utils/index.dart';
import 'package:heven/widget/button.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:heven/widget/toast.dart';

class LoginListPage extends StatefulWidget {
  const LoginListPage({super.key});

  @override
  State<LoginListPage> createState() => _LoginListPageState();
}

class _LoginListPageState extends State<LoginListPage> {
  late FToast fToast;
  void initState() {
    super.initState();
    fToast = FToast();
    // if you want to use context from globally instead of content we need to pass navigatorKey.currentContext!
    fToast.init(context);
  }

  Future<void> _signInWithApple() async {
    // 登录逻辑
    try {
      final credential = await SignInWithApple.getAppleIDCredential(
        scopes: [
          AppleIDAuthorizationScopes.email,
          AppleIDAuthorizationScopes.fullName,
        ],
      );
      print('用户ID: ${credential}');
      var uuid = await Common.getStorageValue('uuid');
      final result =
          await signApple({"udid": credential.userIdentifier, 'uuid': uuid});
      print(result);
      //    ConfigModel configModel = ConfigModel.fromJson(config);
      AppleLoginModel appleLoginModel = AppleLoginModel.fromJson(result);
      if (appleLoginModel.status == 200) {
        print(result['data']);
        await saveUser(result['data'][0]);

        Navigator.pushNamedAndRemoveUntil(
          context,
          '/home', // 你的首页路由名称
          (route) => false, // 移除所有页面
        );
      } else {
        String message = result['message'];
        fToast.showToast(
          child: ToastWidget(text: message),
          gravity: ToastGravity.CENTER,
        );
      }
      // 登录成功后处理逻辑
    } catch (e) {
      print('登录出错: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        padding: const EdgeInsets.fromLTRB(22, 0, 22, 0),
        height: double.infinity,
        width: double.infinity,
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomLeft,
            colors: <Color>[
              Color(0xFF000000),
              Color(0xFF005820),
            ],
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(
              height: 90,
            ),
            Center(
              child: Image.asset(
                'assets/images/logo.png',
                height: 106,
                width: 106,
              ),
            ),
            const SizedBox(
              height: 70,
            ),
            ButtonWidget(
              margin: EdgeInsets.all(0),
              borderRadius: 35,
              child: const Text(
                '手机号登录',
                style: TextStyle(
                  fontSize: 16,
                  color: Color(0xFF000000),
                  fontWeight: FontWeight.w800,
                ),
              ),
              onTap: () {
                Navigator.pushNamed(context, '/login');
              },
            ),
            const SizedBox(
              height: 11,
            ),
            ButtonWidget(
              margin: const EdgeInsets.all(0),
              borderRadius: 35,
              gradient: const LinearGradient(colors: [
                Color(0xFFFFFFFF),
                Color(0xFFFFFFFF),
                Color(0xFFFFFFFF),
              ]),
              child: const Text(
                ' Apple登录',
                style: TextStyle(
                  fontSize: 16,
                  color: Color(0xFF000000),
                  fontWeight: FontWeight.w800,
                ),
              ),
              onTap: () {
                _signInWithApple();
              },
            ),
            const SizedBox(
              height: 20,
            ),
            const Text(
              '我已阅读并接受《用户协议》与《隐私政策》',
              style: TextStyle(
                fontSize: 11,
                color: Color(0xFFE3E3E3),
              ),
            )
          ],
        ),
      ),
    );
  }
}
